#include <iostream>
#include <queue>
#include <map>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <set>
#include <unordered_set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const int N=2e5+10;
int a[N];
int l1[N];
int l2[N];
int w1[N];
int w2[N];
int f[N];
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n;
		cin>>n;
		for(int i=1;i<=n;i++) cin>>a[i];
		for(int i=1;i<=n;i++)
		{
			if(l1[a[i]]==0)
			{
				l1[a[i]]=i;
			}
			else
			{
				w1[i]=i-l1[a[i]]+1;
				if(l2[a[i]]==0) l2[a[i]]=l1[a[i]];
				else
				{
					w2[i]=i-l2[a[i]]+1;
					l2[a[i]]=l1[a[i]];
				}
				l1[a[i]]=i;
			}
		}
		for(int i=1;i<=n;i++)
		{
//			cout<<w2[i]<<' ';
			f[i]=f[i-1];
			int t1=0,t2=0;
			if(w1[i]) t1=f[i-w1[i]+1]==f[i-w1[i]]?0:1;
			if(w2[i]) t2=f[i-w2[i]+1]==f[i-w2[i]]?0:1;
			f[i]=max(f[i],f[i-w1[i]+1]+w1[i]-t1);
			f[i]=max(f[i],f[i-w2[i]+1]+w2[i]-t2);
//			cout<<f[i]<<' '
		}
		cout<<f[n]<<endl;
		for(int i=0;i<=n;i++)
		{
			f[i]=0;
			l1[i]=0;
			w1[i]=0;
			l2[i]=0;
			w2[i]=0;
		}

	}

	return 0;
}

